import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/mode/master/file_info_list.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/image_url.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:loxen/viewmode/file_info_viewmodel.dart';

class FileInspirationList extends StatelessWidget {
  const FileInspirationList({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ScrollController _scrollController = ScrollController();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
        context.read<FileInfoViewModel>().nextData();
      }
    });
    return Selector<FileInfoViewModel, List<FileInfo>>(
      builder: (context, list, _) {
        return Column(
          children: <Widget>[
            Expanded(
                child: StaggeredGridView.countBuilder(
              controller: _scrollController,
              scrollDirection: Axis.vertical,
              padding: const EdgeInsets.fromLTRB(18.0, 23.5, 18.0, 80.0),
              crossAxisCount: 4,
              itemCount: list.length,
              itemBuilder: (context, index) {
                var entity = list[index];
                return GestureDetector(
                  onTap: () {
                    bool select = context.read<FileInfoViewModel>().isDelete;
                    if (select) {
                      context.read<FileInfoViewModel>().selectEntity(entity);
                    } else {
                      Navigator.of(context).pushNamed(Routes.inspiration_detail,
                          arguments: InspirationInfo(
                              id: entity.id,
                              like_num: 0,
                              is_like: 0,
                              is_collect: 0,
                              collect_num: 0));
                    }
                  },
                  child: Stack(
                    alignment: Alignment.center,
                    overflow: Overflow.clip,
                    fit: StackFit.expand,
                    children: <Widget>[
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        mainAxisSize: MainAxisSize.max,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          entity.cover?.trim() != ''
                              ? Expanded(
                                  child: Container(
                                  width: double.infinity,
                                  child: ClipRRect(
                                      borderRadius: BorderRadius.circular(5),
                                      child: CachedNetworkImage(
                                        imageUrl: ImageUrlUtil.getClean(entity.cover),
                                        fit: BoxFit.cover,
                                      )),
                                ))
                              : _defaultItemView(),
                          Container(
                            margin: EdgeInsets.only(top: 28.h, right: 6.w, left: 6.w),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              mainAxisSize: MainAxisSize.max,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text(
                                  entity.title,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                      color: Color(0xFF000000),
                                      fontWeight: FontWeight.w500,
                                      fontSize: 24.sp),
                                ),
                                Text(
                                  "来自于${entity.user}",
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: Color(0xFF82868E),
                                    fontWeight: FontWeight.w500,
                                    fontSize: 20.sp,
                                  ),
                                ),
                              ],
                            ),
                          )
                        ],
                      ),
                      Align(
                        alignment: Alignment.topRight,
                        child: Builder(
                            builder: (context) => context.watch<FileInfoViewModel>().isDelete
                                ? Selector<FileInfoViewModel, bool>(
                                    builder: (_, data, __) {
                                      return Padding(
                                        padding: EdgeInsets.only(
                                          top: 20.w,
                                          right: 20.w,
                                        ),
                                        child: data
                                            ? Image.asset(
                                                'images/select@2x.png',
                                                width: 28.w,
                                                height: 28.w,
                                              )
                                            : Container(
                                                width: 28.w,
                                                height: 28.w,
                                                decoration: BoxDecoration(
                                                    color: Theme.of(context).backgroundColor,
                                                    border: Border.all(
                                                        color: Colors.black26, width: 0.5)),
                                              ),
                                      );
                                    },
                                    selector: (_, __) => entity.select == 1)
                                : Constants.empty),
                      )
                    ],
                  ),
                );
              },
              staggeredTileBuilder: (int index) => new StaggeredTile.count(2, index.isOdd ? 2 : 3),
              mainAxisSpacing: 25.0,
              crossAxisSpacing: 15.0,
            )),
            Builder(
              builder: (context) => context.watch<FileInfoViewModel>().isDelete
                  ? Row(
                      children: <Widget>[
                        Expanded(
                          child: GestureDetector(
                            onTap: () async {
                              bool delete = await showDialog<bool>(
                                  context: context,
                                  builder: (context) {
                                    return AlertDialog(
                                        title: Text(StringResources.ti_shi),
                                        content: Text(
                                          '是否确认移除收藏的灵感？',
                                          style: TextStyle(
                                              color: Theme.of(context).textTheme.subtitle2.color),
                                        ),
                                        actions: <Widget>[
                                          FlatButton(
                                            child: Text(StringResources.qu_xiao),
                                            onPressed: () => Navigator.of(context).pop(),
                                          ),
                                          FlatButton(
                                            child: Text(StringResources.shan_chu),
                                            onPressed: () {
                                              Navigator.of(context).pop(true);
                                            },
                                          ),
                                        ]);
                                  });
                              if (delete != null && delete) {
                                context.read<FileInfoViewModel>().deleteData();
                              }
                            },
                            child: Container(
                              width: double.infinity,
                              height: 98.h,
                              decoration: BoxDecoration(
                                color: Color(0xfff2f2f2),
//                                border: Border.all(
//                                    color: Colors.black26, width: 0.5),
                              ),
                              child: Center(
                                child: Row(
                                  mainAxisSize: MainAxisSize.min,
                                  children: <Widget>[
//                                    Image.asset(
//                                      'images/trash_can.png',
//                                      width: 32.w,
//                                      height: 32.w,
//                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(left: 19.w),
                                      child: Text(StringResources.shan_chu),
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              context.read<FileInfoViewModel>().isDelete = false;
                            },
                            child: Container(
                              width: double.infinity,
                              height: 98.h,
                              decoration: BoxDecoration(
                                color: Color(0xff0cb7d9),
//                                border: Border.all(
//                                    color: Colors.black26, width: 0.5),
                              ),
                              child: Center(
                                child: Text(
                                  StringResources.qu_xiao,
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    )
                  : Constants.empty,
            )
          ],
        );
      },
      selector: (_, model) => model.list,
    );
  }

  _defaultItemView() {
    return Container(
        decoration: BoxDecoration(
          color: Color(0xFF0CB7D9),
          borderRadius: BorderRadius.circular(0),
        ),
        child: Align(
          child: Image.asset(
            'images/default_folder.png',
            width: 27.5,
          ),
        ));
  }
}
